import { ref } from 'vue'
import { useIntersectionObserver } from '@vueuse/core'
/**
 * 封装数据懒加载
 * @param {*} fn
 * @returns
 */
const useLazyData = (fn) => {
  const target = ref(null)
  const targetIsVisible = ref(false)

  const { stop } = useIntersectionObserver(
    target,
    ([{ isIntersecting }], observerElement) => {
      targetIsVisible.value = isIntersecting
      if (isIntersecting) {
        stop()
        fn()
      }
    },
    {
      threshold: 0
    }
  )

  return target
}
export default useLazyData
